<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <h1 id="showTime"></h1>


    <script>


        var oShowTime = document.getElementById('showTime');


        // 页面打开的时候，立即执行一次
        oShowTime.innerHTML = formatDate();

        setInterval(function () {
            oShowTime.innerHTML = formatDate();
        }, 1000)



        // 格式化日期
        function formatDate() {
            var date = new Date();

            var year = date.getFullYear();
            console.log(year);

            // 月份从0开始
            var month = date.getMonth() + 1;
            console.log(month);

            // getDate()  获取日期
            var day = date.getDate();
            console.log(day);

            // getDay()  获取星期
            //   也是从0开始，星期天是0
            var week = date.getDay();
            console.log(week);

            var hour = date.getHours();
            console.log(hour);

            var minute = date.getMinutes();
            console.log(minute);

            var second = date.getSeconds();
            console.log(second);


            // 星期
            var arr = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];


            return year + '-' + month + '-' + day + ' ' + arr[week] + ' ' + hour + ':' + minute + ':' + second
        }

    </script>

</body>

</html>